package mark.tom.friend.presenter;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import mark.tom.friend.bean.bo.AppInfoBO;
import mark.tom.friend.bean.bo.AppConfigBO;
import mark.tom.friend.bean.db.AppConfigDO;
import mark.tom.friend.bean.db.AppConfigDODao;
import mark.tom.friend.bean.db.DaoSession;
import mark.tom.friend.consts.BundleKeyConsts;
import mark.tom.friend.contract.AppInfoContract;

public class AppInfoPresenter implements AppInfoContract.Presenter {

    public final static int REQUEST_CODE_SERVER_CONFIG = 1001;
    public final static int REQUEST_CODE_EXTRA_CONFIG = 1002;

    private AppInfoContract.View mView;
    private AppConfigDODao appConfigDODao;

    @Autowired(name = BundleKeyConsts.BUNDLE_KEY_APP_INFO) public AppInfoBO appInfo;

    @Inject AppInfoPresenter(AppInfoContract.View mView, DaoSession mDaoSession) {
        this.mView = mView;
        this.appConfigDODao = mDaoSession.getAppConfigDODao();
    }

    @Override
    public void onStart() {
        // 初始化界面基本信息
        mView.showAppConfigInfo(appInfo);
    }

    @Override
    public Observable<AppConfigBO> directToConfigPage(final @BundleKeyConsts.AppConfigGroup String groupName) {
        return Observable.just(groupName)
                .map(new Function<String, AppConfigDO>() {
                    @Override
                    public AppConfigDO apply(@NonNull String s) {
                        return appConfigDODao.queryBuilder()
                                .where(AppConfigDODao.Properties.PackageName.eq(appInfo.getPackageName()))
                                .uniqueOrThrow();
                    }
                })
                .map(new Function<AppConfigDO, AppConfigBO>() {
                    @Override
                    public AppConfigBO apply(@NonNull AppConfigDO appSettingDO) {
                        return new AppConfigBO.AppConfigDOConvert().convert(appSettingDO);
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    @Override
    public Observable<AppConfigBO> loadAppDebugInfo() {
        return Observable.just(appInfo.getPackageName())
                .map(new Function<String, AppConfigBO>() {
                    @Override
                    public AppConfigBO apply(@NonNull String packageName) {
                        AppConfigBO appSettingBO;
                        AppConfigDO appSettingDO = appConfigDODao.queryBuilder()
                                .where(AppConfigDODao.Properties.PackageName.eq(packageName))
                                .unique();
                        if (null == appSettingDO) {
                            appSettingBO = new AppConfigBO();
                        } else {
                            appSettingBO = new AppConfigBO.AppConfigDOConvert().convert(appSettingDO);
                        }
                        return appSettingBO;
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    @Override
    public Observable<Boolean> updateAppDebugAbleStatus(boolean debugAble) {
        return Observable.just(debugAble)
                .doOnNext(new Consumer<Boolean>() {
                    @Override
                    public void accept(@NonNull Boolean debugAble) {
                        AppConfigDO appSettingDO = appConfigDODao.queryBuilder()
                                .where(AppConfigDODao.Properties.PackageName.eq(appInfo.getPackageName()))
                                .unique();
                        if (null == appSettingDO) {
                            appSettingDO = new AppConfigDO();
                            appSettingDO.setPackageName(appInfo.getPackageName());
                        }
                        appSettingDO.setDebugAble(debugAble);
                        appConfigDODao.save(appSettingDO);
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    @Override
    public Observable<String> onResult(final int requestCode, Object data) {
        String content = null;
        switch (requestCode) {
            case REQUEST_CODE_SERVER_CONFIG:
            case REQUEST_CODE_EXTRA_CONFIG: {
                content = (String) data;
                break;
            }
            default: {
                break;
            }
        }
        if (null == content) {
            return null;
        }
        return Observable.just(content)
                .doOnNext(new Consumer<String>() {
                    @Override
                    public void accept(@NonNull String s) {
                        AppConfigDO appSettingDO = appConfigDODao.queryBuilder()
                                .where(AppConfigDODao.Properties.PackageName.eq(appInfo.getPackageName()))
                                .uniqueOrThrow();
                        if (REQUEST_CODE_SERVER_CONFIG == requestCode) {
                            appSettingDO.setServerConfig(s);
                        } else {
                            appSettingDO.setExtraConfig(s);
                        }
                        appConfigDODao.update(appSettingDO);
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    @Override
    public void onDestroy() {
        mView = null;
    }

}
